#!/bin/sh

################################################################################
##                                                                            ##
## Copyright (c) International Business Machines  Corp., 2005                 ##
##                                                                            ##
## This program is free software;  you can redistribute it and#or modify      ##
## it under the terms of the GNU General Public License as published by       ##
## the Free Software Foundation; either version 2 of the License, or          ##
## (at your option) any later version.                                        ##
##                                                                            ##
## This program is distributed in the hope that it will be useful, but        ##
## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ##
## or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License   ##
## for more details.                                                          ##
##                                                                            ##
## You should have received a copy of the GNU General Public License          ##
## along with this program;  if not, write to the Free Software               ##
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA    ##
##                                                                            ##
##                                                                            ##
################################################################################
#
# File:
#   get_ifname
#
# Description:
#   Get the interface name which belongs to the specified test link
#
# Author:
#   Mitsuru Chinen <mitch@jp.ibm.com>
#
# Arguments:
#   $1: Set the host type to set the IPv4 address
#       lhost - local host / rhost - remote host
#   $2: The number of the test link
#
# History:
#   Oct 19 2005 - Created (Mitsuru Chinen)
#
#-----------------------------------------------------------------------
#Uncomment line below for debug output.
#trace_logic=${trace_logic:-"set -x"}
$trace_logic

# Make sure the value of LTPROOT
LTPROOT=${LTPROOT:-`(cd ../../ ; pwd)`}
TMPDIR=${TMPDIR:-/tmp}
export LTPROOT TMPDIR

# Check the environment variable for the test
. check_envval || exit 1

# Arguments
if [ $# -ne 2 ]; then
    echo "Usage: $0 host_type link_num" >&2
    exit 1
fi
host_type=$1
link_num=$2

# Check the host type
case $host_type in
    lhost)
    hwaddrs="$LHOST_HWADDRS"
    ;;

    rhost)
    hwaddrs="$RHOST_HWADDRS"
    ;;

    *)
    echo "$0: 1st argument must be lhost or rhost" >&2
    exit 1
    ;;
esac

# Pick HWaddr from HWaddr list
field=`expr $link_num + 1`
hwaddr=`echo $hwaddrs | cut -d ' ' -f $field`
if [ x${hwaddr} = x ]; then
    echo "HWaddr list ($hwaddrs) is something wrong." >&2
    exit 1
fi

ip_link_show_out=`mktemp $TMPDIR/tmp.XXXXXXXX`
if [ $host_type = lhost ]; then
    ip link show > $ip_link_show_out 2>&1
else
    $LTP_RSH $RHOST 'PATH=/sbin:/usr/sbin:$PATH ip link show' \
		> $ip_link_show_out 2>&1
fi
ifname=`grep -1 -i $hwaddr $ip_link_show_out | head -n 1 | awk '{ print $2 }' | sed "s/://"` 2>/dev/null
rm -f $ip_link_show_out

# Detect a interface name from the HWaddr
if [ x$ifname = x ]; then
    echo "Interface which has $hwaddr is not found." >&2
    exit 1
fi

echo $ifname
exit 0
